package com.ruoyi.ys7Video.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.Threads;
import com.ruoyi.ys7Video.domain.DeviceInfo;
import com.ruoyi.ys7Video.domain.VideoDao;
import com.ruoyi.ys7Video.domain.VideoTokenDao;
import com.ruoyi.ys7Video.mapper.VideoMapper;
import com.ruoyi.ys7Video.service.VideoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Mr-Le
 */
@Service
@Slf4j
public class VideoServiceImpl implements VideoService {

    @Autowired
    private VideoMapper videoMapper;

    /**
     * 萤石云 devices Url
     */
    @Value("${ys7.ys7DevicesList}")
    private String ys7DevicesList;


    /**
     * 新增 token 记录
     *
     * @param videoDao
     * @return
     */
    @Override
    public int insertVideoUrl(VideoDao videoDao) {
        videoDao.setUpdateTime(DateUtils.getNowDate());
        return videoMapper.insertVideoUrl(videoDao);
    }

    /**
     * 查询最后一个视频设备状态
     *
     * @return
     */
    @Override
    public VideoDao selectVideoLast() {
        return videoMapper.selectVideoLast();
    }

    /**
     * 更新最后一个视频设备状态
     *
     * @return
     */
    @Override
    public int updateVideoLast(VideoDao videoDao) {
        return videoMapper.updateVideoLast(videoDao);
    }


    /**
     * 更新视频 Token
     *
     * @param videoTokenDao
     * @return
     */
    @Override
    public int updateToken(VideoTokenDao videoTokenDao) {
        return videoMapper.updateToken(videoTokenDao);
    }

    /**
     * 更新过期时间
     *
     * @param videoTokenDao
     * @return
     */
    @Override
    public int updateTokenExpireTime(VideoTokenDao videoTokenDao) {
        return videoMapper.updateTokenExpireTime(videoTokenDao);
    }

    /**
     * 查找视频设备列表
     *
     * @param videoDao
     * @return
     */
    @Override
    public List<VideoDao> selectVideoDevice(VideoDao videoDao) {
        return videoMapper.selectVideoDevice(videoDao);
    }


    /**
     * 插入抓拍图片
     *
     * @param videoDao url数据信息
     * @return 影响行数
     */
    @Override
    public int insertCaptureImg(VideoDao videoDao) {
        return videoMapper.insertCaptureImg(videoDao);
    }

    /**
     * 查抓拍图片列表
     * @param deviceSerial 设备序列号
     * @return 查询列表
     */
    @Override
    public List<VideoDao> selectCaptureImgList(String deviceSerial) {
        return videoMapper.selectCaptureImgList(deviceSerial);
    }

    /**
     * 查询设备列表
     * @return 返回消息
     */
    @Override
    public List<DeviceInfo> ys7DeviceList() {
        VideoDao videoLast = selectVideoLast();
        /* 拼接获取token的URL */
        String getDevicesListUrl = ys7DevicesList + "?accessToken=" + videoLast.getAccessToken();
        List<DeviceInfo> deviceInfos = null;
        try {
            ResponseEntity<String> responseEntity = Threads.postRequest(getDevicesListUrl);
            log.info(responseEntity.getBody());
            JSONObject jsonObject = JSON.parseObject(responseEntity.getBody());
            Object data = jsonObject.get("data");
            /* 拿到data数据 */
             deviceInfos = JSON.parseArray(data.toString(), DeviceInfo.class);
            assert deviceInfos != null;
            log.info(String.valueOf(deviceInfos.size()));
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return deviceInfos;
    }
}
